<?php
/*
	多页小程序
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
if($_RQ['basicname']){
	$_SESSION['plugin_name'] = $_RQ['basicname'];
}
if($_SESSION['plugin_name']){
	if($_SESSION['plugin_name'] !='core'){
		$_SESSION['actiontypes']=cfc($_SESSION['plugin_name'])->actiontypes();
		if(empty($_SESSION['actiontypes']) && $_SESSION['plugin_name'] !='core'){
			$types =array();
			$plugins = pdo_getall('core_plugins',array('basic'=>$_SESSION['plugin_name']),array('name','title'));
			if(!empty($plugins)){
				foreach($plugins as $p){
					$isdiy = pdo_getall('core_mobile_blocks',array('type'=>1,'plugin_id'=>$p['name']));
					$install = check_permission($p['name']);
					if(!empty($isdiy) && $install){
						if (method_exists(cfc($p['name']),'getactiontypes')){
							$actiontypes = cfc($p['name'])->getactiontypes();
								foreach($actiontypes as $u=>$t){
									$types[$p['name'].'/'.$u]=$p['title'].'-'.$t;
								}
						}
					}
				}
			}
			$_SESSION['actiontypes'] = $types;
		}
		$_SESSION['actiontypes'][diypage] = 'DIY页面';
	}else{
		$_SESSION['actiontypes'] = array();
		$_SESSION['actiontypes'][diypage] = 'DIY页面';
	}
}
exi('','','core/wxapp.list/list/basicname='.$_SESSION['plugin_name']);
$return['actiontypes']=$_SESSION['actiontypes'] ? $_SESSION['actiontypes'] : array();
if($_RQ['op']=='get'){
	$list = array(
			array('title'=>'全网小程序DIY（下面是支持多页DIY的小程序类型）','list'=>array(
					array('name'=>'core','title'=>'微官网DIY小程序','desc'=>'微官网/企业官网DIY小程序','icon'=>''),
					array('name'=>'jy_weishop','title'=>'微商城DIY小程序','desc'=>'智慧电商客DIY小程序','icon'=>''),
					array('name'=>'jy_yuyue','title'=>'微预约DIY小程序','desc'=>'全网预约DIY小程序','icon'=>''),
					array('name'=>'jy_cms','title'=>'地方门户DIY小程序','desc'=>'CMS地方门户DIY小程序','icon'=>''),
					array('name'=>'jy_bbs','title'=>'智慧社圈DIY小程序','desc'=>'智慧社圈DIY小程序','icon'=>''),
					array('name'=>'jy_dish','title'=>'微点餐DIY小程序','desc'=>'轻松点餐DIY小程序','icon'=>''),
					array('name'=>'jy_jd','title'=>'微酒店DIY小程序','desc'=>'智慧酒店DIY小程序','icon'=>''),
					array('name'=>'jy_meifa','title'=>'共享美发DIY小程序','desc'=>'共享美发DIY小程序','icon'=>''),
					array('name'=>'jy_gohome','title'=>'上门服务DIY小程序','desc'=>'上门服务DIY小程序','icon'=>''),
			)),
	);
	foreach($list as &$type){
		foreach($type['list'] as &$p){
			$install = check_permission($p['name']);
			if($install || $p['name']=='core'){
				$p['is_install'] = 1;
				$p['is_show'] = 1;
			}else{
				$p['is_install'] = 0;
				$p['is_show'] = 0;
				if($_SESSION['isfounder']){
					$p['is_show'] = 1;
				}
			}
		}
	}
	$return['item']['plugins']=$list;
	$htmls=array(
		'type' => 'look',
		'look' => array(
			array('type'=>'plugins','pluginname'=>'core','action'=>'wxapp.multiple','op'=>'list','name'=>'plugins'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list'){
	//小程序列表
	if($_SESSION['plugin_name'] !='core'){
		$isinstall = pdo_get('core_wxapp',array('name'=>$_SESSION['plugin_name']),array('name','title'));
		if(empty($isinstall)){
			exi('没有安装本应用小程序！','error');
		}
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>8,'plugin_name'=>$_SESSION['plugin_name']);//读取多页小程序
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$fields=array('id','title','type','version','plugin_name','isdiyindex');
	$pindex = max(1, $_RQ['page']);
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_wxapp',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$row){
			$row['fans']=array('action'=>'wxapp.fans','op'=>'list','query'=>'wxapp_id='.$row['id'],'title'=>'粉丝管理');

		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'get','title'=>'全网小程序'),
			array('op'=>'list','title'=>'小程序列表'),
			array('op'=>'edit','title'=>'创建小程序'),
		),
		'list'=>array('data'=>'list','psize'=>$psize,'total'=>$total,'list'=>array(
			array('type'=>'string','title'=>'小程序名称','name'=>'title'),
			array('type'=>'href','title'=>'粉丝管理','name'=>'fans'),
			array('type'=>'modal','title'=>'支付参数设置','op'=>'pay'),
			array('type'=>'href','title'=>'菜单设计','op'=>'menu'),
			array('type'=>'switch','name'=>'isdiyindex','title'=>'是否应用DIY首页','set'=>'diyindex_set'),
			array('type'=>'href','title'=>'下载二维码','op'=>'ewm'),
		),'action'=>array(
			array('type'=>'href','op'=>'page_list','title'=>'页面管理'),
			array('type'=>'href','op'=>'design','title'=>'设计'),
			array('type'=>'href','action'=>'wxapp.list','op'=>'up_desc','title'=>'上传代码'),
			
		)),
		'search_one'=>array('name'=>'title','add_op'=>'edit'),
	);

	$return['htmls']=$htmls;
	set_html('header','common/header');
	set_html('title','小程序');
	exi($return);
}
if($_RQ['op']=='ewm'){
	//exi('开发中！','error');
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$id));
		if(!$wxapp){
			exi('指定小程序不存在！','error');
		}
		if(!$wxapp['appid'] || !$wxapp['appsecret']){
			exi('公众号参数缺失，无法生成小程序二维码！','error');
		}
		$action=trim($post['action']);
		if(!$action){
			exi('请输入页面action','error');
		}
		$action_array=links($action);
		$action_array['action']=str_replace('.','/',$action_array['action']);
		$attachment=pdo_getcolumn('core_account_wxapp_ewm',array('wxapp_id'=>$id,'action'=>$action),'attachment');
		if(!$attachment){
			//$action_dir=str_replace('.','/',$action);
			$page="/{$action_array['plugin']}/{$action_array['action']}/index";
			if($action_array['query']){
				$page.="?".$action_array['query'];
			}
			$data=model('core/wxapp')->appcode($page,$wxapp['appid'],$wxapp['appsecret']);
			$attachment=model('core/files')->source_save($data,'data','jpg');
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'wxapp_id'=>$id,
				'action'=>$action,
				'attachment'=>$attachment,
			);
			pdo_insert('core_account_wxapp_ewm',$insert);
		}
		$ewm_url=tomedia($attachment);
		exi('','','',$ewm_url);
	}
	$id=intval($_RQ['id']);
	$item=array(
		'id'=>$id,
	);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'link','name'=>'action','from'=>'action','title'=>'页面action','help'=>'根据url生成不同的小程序码，填写标准完整的页面action'),
		),
	);
	exi($return);
}
if($_RQ['op']=='edit'){
	//新增编辑多页小程序
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'title'=>$post['title'],
			'desc'=>$post['desc'],
			'logo'=>$post['logo'],
			'appid'=>$post['appid'],
			'appsecret'=>$post['appsecret'],
			'oldid'=>$post['oldid'],
			'setting'=>ijson_encode($post['setting']),
			'sharetitle'=>trim($post['sharetitle']),
			'sharedesc'=>trim($post['sharedesc']),
			'shareimg'=>trim($post['shareimg']),
		);
		if($id){
			$wxapp_id=pdo_getcolumn('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'wxapp_id');
			if(!$wxapp_id && $_SESSION['plugin_name'] !='core'){
				$data['wxapp_id']=pdo_getcolumn('core_wxapp',array('name'=>$_SESSION['plugin_name']),'id');
			}
			pdo_update('core_account_wxapp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			model('core/web')->check_num('wxapp');
			$data['type']=8;
			if($_SESSION['plugin_name'] !='core'){
				$data['wxapp_id']=pdo_getcolumn('core_wxapp',array('name'=>$_SESSION['plugin_name']),'id');
			}
			$data['plugin_name']=$_SESSION['plugin_name'];
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_account_wxapp',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','logo','appid','appsecret','oldid','setting','sharetitle','sharedesc','shareimg'));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$setting =$return['item']['setting'] ? json_decode($return['item']['setting'],true):array('isfooter'=>0);
	$setting['window']=$setting['window']?$setting['window']:array('navigationStyle'=>'');
	$setting['footer']=$setting['footer']?$setting['footer']:array('background'=>'#fff');
	$setting['footer']['menus']=$setting['footer']['menus']?$setting['footer']['menus']:array();
	$setting['window']['navigationBarTitleText']=$setting['window']['navigationBarTitleText']?$setting['window']['navigationBarTitleText']:$return['item']['title'];
	$setting['window']['navigationBarBackgroundColor']=$setting['window']['navigationBarBackgroundColor']?$setting['window']['navigationBarBackgroundColor']:'#f10215';
	$setting['window']['navigationBarTextStyle']=$setting['window']['navigationBarTextStyle']?$setting['window']['navigationBarTextStyle']:'white';
	$return['item']['setting'] = $setting;
	$return['item']['news_num']=$return['item']['shownums']['news_num'];
	$return['item']['stores_num']=$return['item']['shownums']['stores_num'];
	$return['item']['products_num']=$return['item']['shownums']['products_num'];
	$return['item']['anlis_num']=$return['item']['shownums']['anlis_num'];
	$return['navigationBarTextStyles'] =array('white'=>'white','black'=>'black');
	$return['backgroundTextStyles'] =array('dark'=>'dark','light'=>'light');
	$return['borderStyles'] =array('white'=>'white','black'=>'black');
	$return['positions'] =array('bottom'=>'bottom','top'=>'top');
	$return['navigationStyles'] =array('default'=>'default','custom'=>'custom');
	$return['iconstyle'] =array('0'=>'图片图标','1'=>'字体图标');
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置小程序基本参数',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回小程序列表'),
		),
		'edit_tab'=>true,
			'edits'=>array(
					array(
					'title'=>'基本信息','list'=>array(
						array('type'=>'string','name'=>'title','title'=>'小程序名称','must'=>true),
						array('type'=>'image','name'=>'logo','title'=>'小程序logo'),
						array('type'=>'string','title'=>'appid','name'=>'appid','help'=>'填写小程序appid','must'=>true ),
						array('type'=>'string','title'=>'appsecret','name'=>'appsecret','help'=>'填写小程序 appsecret','must'=>true ),
						array('type'=>'string','title'=>'原始ID','name'=>'oldid','help'=>'填写小程序原始ID' ),
						),
					),
					array(
					'title'=>'顶部导航设置','list'=>array(
						array('type'=>'radio','name'=>'setting.window.navigationStyle','title'=>'是否关闭顶部导航','options'=>'navigationStyles','option_value'=>'dtitle','help'=>'导航栏样式，仅支持以下值：default 默认样式custom 自定义导航栏，只保留右上角胶囊按钮'),
						array('type'=>'color','name'=>'setting.window.navigationBarBackgroundColor','title'=>'导航栏背景色','help'=>'小程序顶部背景色'),
						array('type'=>'radio','name'=>'setting.window.navigationBarTextStyle','title'=>'导航栏标题颜色','options'=>'navigationBarTextStyles','option_value'=>'dtitle'),
						array('type'=>'string','name'=>'setting.window.navigationBarTitleText','title'=>'导航栏标题','help'=>'导航栏标题文字内容！留空这使用小程序名称'),
					),
					),
					array(
						'title'=>'窗口样式设置','list'=>array(
							array('type'=>'color','name'=>'setting.window.backgroundColor','title'=>'窗口的背景色','help'=>'默认白色，大部分不用填写！'),
							array('type'=>'radio','name'=>'setting.window.backgroundTextStyle','title'=>'窗口的背景色','options'=>'backgroundTextStyles','option_value'=>'dtitle'),
							array('type'=>'color','name'=>'setting.window.backgroundColorTop','title'=>'顶部窗口的背景色','help'=>'顶部窗口的背景色，仅 iOS 支持,微信版本 6.5.16'),
							array('type'=>'color','name'=>'setting.window.backgroundColorBottom','title'=>'底部窗口的背景色','help'=>'顶部窗口的背景色，仅 iOS 支持,微信版本 6.5.16'),
						),
					),
					array(
						'title'=>'分享设置','list'=>array(
							array('type'=>'string','name'=>'sharetitle','title'=>'小程序分享标题','help'=>'小程序分享标题'),
							array('type'=>'string','name'=>'sharedesc','title'=>'小程序分享描述','help'=>'小程序首页分享描述'),
							array('type'=>'image','name'=>'shareimg','title'=>'小程序分享图片','help'=>'小程序首页分享图片自定义，留空则默认截屏分享！'),
						),
					),
					array(
						'title'=>'其他全局设置','list'=>array(
							array('type'=>'switch','name'=>'setting.window.enablePullDownRefresh','title'=>'是否全局开启下拉刷新'),
							array('type'=>'string','name'=>'setting.window.onReachBottomDistance','title'=>'上拉触底距离','help'=>'只能填数字，默认50，页面上拉触底事件触发时距页面底部距离，单位为px'),
						),
					),
		),
	);
	exi($return);
}
if($_RQ['op']=='pay'){
	//设置小程序支付参数
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择要设置的小程序！','error');
		}
		$data=array(
			'mchid'=>$post['mchid'],
			'paykey'=>$post['paykey'],
		);
		pdo_update('core_account_wxapp',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！');
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要设置的小程序！','error');
		}
		$item=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','mchid','paykey'));
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置小程序支付参数',
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'小程序名称','read'=>true),
				array('type'=>'string','name'=>'mchid','title'=>'微信支付商户号','help'=>'微信支付商户号，从微信商户平台获取（开通微信支付的通知邮件中也有）'),
				array('type'=>'string','name'=>'paykey','title'=>'微信支付密钥','help'=>'设置微信支付密钥，需要和微信商户平台的api密钥保持一致'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='menu'){
	//设计小程序菜单
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择小程序~！','error');
		}
		$setting=ijson_encode($post['setting']);
		pdo_update('core_account_wxapp',array('setting'=>$setting),array('id'=>$id));
		exi('设置成功~！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择小程序~！','error');
	}
	$return['borderStyles'] =array('0'=>'white','1'=>'black');
	$return['positions'] =array('0'=>'bottom','1'=>'top');
	$return['item']=array();
	$return['item']=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$setting =$return['item']['setting'] ? json_decode($return['item']['setting'],true):array('isfooter'=>0);
	$setting['footer']=$setting['footer']?$setting['footer']:array('backgroundColor'=>'#fff');
	$setting['footer']['menus']=$setting['footer']['menus']?$setting['footer']['menus']:array(array('title'=>'首页'));
	$return['item']['setting'] = $setting;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'小程序tab设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回小程序列表'),
		),
		'edit'=>array(
							array('type'=>'color','name'=>'setting.footer.color','title'=>'文字默认颜色','help'=>'默认黑色'),
							array('type'=>'color','name'=>'setting.footer.selectedColor','title'=>'选中时的颜色','help'=>'默认绿色'),
							array('type'=>'color','name'=>'setting.footer.backgroundColor','title'=>'导航的背景色','help'=>'默认白色'),
							array('type'=>'radio','name'=>'setting.footer.borderStyle','title'=>'边框的颜色','options'=>'borderStyles','option_value'=>'dtitle','help'=>'tabbar上边框的颜色， 仅支持 black / white'),
							array('type'=>'radio','name'=>'setting.footer.position','title'=>'TabBar的位置','options'=>'positions','option_value'=>'dtitle','help'=>'选择top的话，底部就不显示图片了，tabBar的位置，仅支持 bottom / top'),
							//array('type'=>'switch','name'=>'istabbar','title'=>'是开启小程序导航','help'=>'默认使用微官网自定义导航！'),
							array(
								'type'=>'array',
								'name'=>'menus',
								'tovalue'=>'pagedata.item.setting.footer',
								//'show'=>array('istabbar'=>1),
								'add_tip'=>'添加一个底部导航',
									'list'=>array(
												array('type'=>'string','name'=>'title','title'=>'菜单名称'),
												array('type'=>'icon','name'=>'icon','title'=>'默认图标'),
												array('type'=>'string','name'=>'url','title'=>'菜单链接'),
									),
									'details'=>array(
												array('type'=>'string','name'=>'title','title'=>'菜单名称'),
												array('type'=>'icon','name'=>'icon','title'=>'默认图标'),
												array('type'=>'choose','op'=>'page_choose','name'=>'url','title'=>'菜单链接'),
												
									)
							),
		),
	);
	exi($return);
}

if($_RQ['op']=='del'){
	$dels=array();
	
	if($_RQ['del']){
		$dels=str_array($_RQ['del']);
	}
	$id=intval($_RQ['id']);
	if($id){
		$dels[]=$id;
	}
	if($dels){
		foreach($dels as $id){
			pdo_delete('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='page_list'){
	$id=intval($_RQ['id']);
	if($id){
		$_SESSION['wxapp_id']=$id;
	}
	if(!$_SESSION['wxapp_id']){
		exi('请先选择小程序！','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'wxapp_id'=>$_SESSION['wxapp_id']);//读取多页小程序
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$fields=array('id','is_index','name','title');
	$pindex = max(1, $_RQ['page']);
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_wxapp_pages',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回小程序列表'),
			array('op'=>'page_add','title'=>'添加页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'page_del','list'=>array(
			array('name'=>'name','title'=>'页面类型','type'=>'radio','options'=>'actiontypes'),
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			array('type'=>'switch','name'=>'is_index','title'=>'是否作为首页','set'=>'page_set'),
			array('type'=>'href','op'=>'design','query'=>'ispage=1','title'=>'设计'),
		),'action'=>array(
			array('type'=>'href','op'=>'page_add','title'=>'编辑'),
			
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='page_set'){
	//设置是否作为首页
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择小程序~！','error');
	}
	$post=get_postdata();
	$is_index=intval($post['is_index']);
	if($is_index){
		pdo_update('core_account_wxapp_pages',array('is_index'=>0),array('wxapp_id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('core_account_wxapp_pages',array('is_index'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		pdo_update('core_account_wxapp_pages',array('is_index'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('设置成功！','error');
}
if($_RQ['op']=='diyindex_set'){
	//设置是否作为首页
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择小程序~！','error');
	}
	$post=get_postdata();
	$isdiyindex=intval($post['isdiyindex']);
	pdo_update('core_account_wxapp',array('isdiyindex'=>$isdiyindex),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('设置成功！','error');
}
if($_RQ['op']=='page_add'){
	
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'wxapp_id'=>$_SESSION['wxapp_id'],
			'title'=>trim($post['title']),
			'name'=>trim($post['name']),
		);
		if(!$data['title']){
			exi('请填写页面名称！','error');
		}
		if($id){
			pdo_update('core_account_wxapp_pages',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			pdo_insert('core_account_wxapp_pages',$data);
			$id=pdo_insertid();
		}
		exi('创建成功！点击确定进入设计页面！','','//design/ispage=1&id='.$id);
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_wxapp_pages',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','name'));
	}else{
		$item=array('title'=>'');
	}
	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'page_list','title'=>'返回页面列表'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			array('type'=>'radio','name'=>'name','title'=>'页面类型','options'=>'actiontypes'),
		),
	);
	exi($return);
}
if($_RQ['op']=='page_del'){
	$dels=array();
	
	if($_RQ['del']){
		$dels=str_array($_RQ['del']);
	}
	$id=intval($_RQ['id']);
	if($id){
		$dels[]=$id;
	}
	if($dels){
		foreach($dels as $id){
			pdo_delete('core_account_wxapp_pages',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='page_choose'){
	//选择链接
	if(!$_SESSION['wxapp_id']){
		exi('请先选择小程序！','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'wxapp_id'=>$_SESSION['wxapp_id']);//读取多页小程序
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$fields=array('id','name','title');
	$pindex = max(1, $_RQ['page']);
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_wxapp_pages',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$list?$list:array();
	foreach($list as &$item){
		$item['action']='core/diy.page_'.$item['id'].'/';
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'page_choose','title'=>'本小程序页面'),
			array('op'=>'syspage_choose','title'=>'小程序系统页面'),
			array('op'=>'other_choose','title'=>'小程序其他页面'),
			array('action'=>'tools.link','title'=>'系统H5页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'name','title'=>'页面类型','type'=>'radio','options'=>'actiontypes'),
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			array('name'=>'action','title'=>'页面路径','type'=>'string'),
			
		),'action'=>array(
			array('type'=>'modal_result','name'=>'action','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='syspage_choose'){
	//选择链接
	if(!$_SESSION['wxapp_id']){
		exi('请先选择小程序！','error');
	}
	$list=get_wxapp_pages();
	$list=$list?$list:array();
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'page_choose','title'=>'本小程序页面'),
			array('op'=>'syspage_choose','title'=>'小程序系统页面'),
			array('op'=>'other_choose','title'=>'小程序其他页面'),
			array('action'=>'tools.link','title'=>'系统H5页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			array('type'=>'string','name'=>'url','title'=>'页面路径'),	
		),'action'=>array(
			array('type'=>'modal_result','name'=>'url','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='other_choose'){
	//选择链接
	if(!$_SESSION['wxapp_id']){
		exi('请先选择小程序！','error');
	}
	$list=get_wxapp_shoppages();
	$list=$list?$list:array();
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'page_choose','title'=>'本小程序页面'),
			array('op'=>'syspage_choose','title'=>'小程序系统页面'),
			array('op'=>'other_choose','title'=>'小程序其他页面'),
			array('action'=>'tools.link','title'=>'系统H5页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			array('type'=>'string','name'=>'url','title'=>'页面路径'),	
		),'action'=>array(
			array('type'=>'modal_result','name'=>'url','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='design'){
	//设计小程序：设计小程序页面，适用于diy小程序
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择微站！','error');
		}
		
		$update=array(
			'items'=>$post['items']?ijson_encode($post['items']):'',
			'setting'=>$post['setting']?ijson_encode($post['setting']):''
		);
		//检测恶意代码
		if(strexists($update['items'],'<?') || strexists($update['setting'],'<?')){
			exi('提交内容存在恶意代码，无法提交！','error');
		}
		if($post['ispage']){
			pdo_update('core_account_wxapp_pages',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			exi('保存成功！','','/wxapp.multiple/page_list/id='.$_SESSION['wxapp_id']);
		}else{
			pdo_update('core_account_wxapp',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			exi('保存成功！','','goback');
		}
		
	}else{
		$id=intval($_RQ['id']);
		$basictype=$_RQ['basictype']?intval($_RQ['basictype']) : 0 ;
		if(!$id){
			exi('请先选择要设计的小程序！','error');
		}
		$return['used_navs']=array();
		if($_RQ['ispage']){
			$item=pdo_get('core_account_wxapp_pages',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','items','setting','title'));
			$item['ispage']=1;
		}else{
			$item=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','items','setting','title'));
			$_SESSION['wxapp_id']=$id;
		}
		
		$item['items']=$item['items']?json_decode($item['items'],true):array();
		if($item['items']){
			foreach($item['items'] as $row){
				if(!$return['used_navs'][$row['nid']]){
					$return['used_navs'][$row['nid']]=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['nid']),array('name','showhtml','edithtml'));
				}
			}
		}
		$item['setting']=$item['setting']?json_decode($item['setting'],true):array('bgimg'=>'');
		$navs0=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>0,'support'=>0),array(),'','usenum desc',array('10'));
		if($navs0){
			foreach($navs0 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
				
			}
		}
		$navs1=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>1,'support'=>0),array(),'','usenum desc',array('10'));
		if($navs1){
			foreach($navs1 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
				
			}
		}

		if($_SESSION['plugin_name'] != 'core'){
			$plugins[0]=array('name'=>$_SESSION['plugin_name']);
			if (method_exists(cfc($_SESSION['plugin_name']),'getplugins')){
				$getplugins = cfc($_SESSION['plugin_name'])->getplugins();
				foreach($getplugins as $type){
					foreach($type['list'] as $tp){
						$plugins[]=array('name'=>$tp['name']);
					}
				}
			}
		}else{
			$plugins=array();
		}
		
		if($plugins){
			$plugintitles=array();
			$navs2=array();
			$i = 0;
			foreach($plugins as $k=>&$p){
				$plugintitle=pdo_getcolumn('core_plugins',array('name'=>$p['name']),'title');
				if (in_array($plugintitle,$plugintitles)){
					
				}else{
					$plugintitles[$i] = $plugintitle;
					$blocks = pdo_getall('core_mobile_blocks',array('type'=>1,'support <>'=>0,'plugin_id'=>$p['name']),array(),'','usenum desc',array('10'));
					if($blocks){
						foreach($blocks as &$b){
							$b['default']=json_decode($b['default'],true);
						}
						$navs2[$i]['name'] = $p['plugin_id'];
						$navs2[$i]['plugintitle']=$plugintitle;
						$navs2[$i]['navs'] = $blocks;
						$i  += 1;
					}
				}
				
			}
		}
		
		$return['navs0']=$navs0;
		$return['navs1']=$navs1;
		$return['navs2']=$navs2;
		$menus =array(
				array('title'=>'首页','iconimg'=>'../core/resource/images/diy/icon/gray_home.png','choseimg'=>'../core/resource/images/diy/icon/green_home.png','url'=>'index'),
				array('title'=>'产品','iconimg'=>'../core/resource/images/diy/icon/gray_cate.png','choseimg'=>'../core/resource/images/diy/icon/green_cate.png','url'=>'goods'),
				array('title'=>'新闻','iconimg'=>'../core/resource/images/diy/icon/gray_find.png','choseimg'=>'../core/resource/images/diy/icon/green_find.png','url'=>'news'),
				array('title'=>'我的','iconimg'=>'../core/resource/images/diy/icon/gray_people.png','choseimg'=>'../core/resource/images/diy/icon/green_people.png','url'=>'member'),
		);
		$item['setting']['window']['navigationBarTitleText']=$item['setting']['window']['navigationBarTitleText']?$item['setting']['window']['navigationBarTitleText']:$item['title'];
		$item['setting']['window']['navigationBarBackgroundColor']=$item['setting']['window']['navigationBarBackgroundColor']?$item['setting']['window']['navigationBarBackgroundColor']:'#ff0000';
		$item['setting']['footer']['iconstyle']=$item['setting']['footer']['iconstyle']?$item['setting']['footer']['iconstyle']:0;
		$item['setting']['footer']['position']=$item['setting']['footer']['position']?$item['setting']['footer']['position']:'bottom';
		$item['setting']['footer']['menus']=!empty($item['setting']['footer']['menus']) ? $item['setting']['footer']['menus']:$menus;
		$item['setting']['bg'] = $item['setting']['bg'] ? $item['setting']['bg'] : array();
		$return['item']=$item;
		$return['edit']=3;
		$return['siteroot']=get_account_url();
		$return['account']=$_SESSION['account'];
		$return['extradata']=array();
		exi($return);
	}
}
if($_RQ['op']=='getnav'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	$navs=pdo_getall('core_mobile_blocks',$params,array(),'','usenum desc',array('10'));
	exi($navs);
}